package com.cloudsigns.factorymanager2.engineering.model;

import android.text.TextUtils;

import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.utils.EngineeringUtil;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.ProjectUtil;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/3.
 */

public class EngineeringOrderListForMapModel implements InEngineeringOrderListForMapModel {

    @Override
    public void getEngineeringOrderListForMap(final OnEngineeringOrderListForMapListener engineeringOrderListForMapListener) {
        new Thread(new Runnable() {
            @Override
            public void run() {


                HttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(ProjectUtil.ENGINEERING_ORDER_LIST_FOR_MAP_URL);

                HttpResponse response;

                try {
                    httpGet.addHeader("token", MyApplicaton.getApplication().getToken());
                    httpGet.addHeader(HTTP.CONTENT_TYPE, ConstantUtil.CONTENT_TYPE);

                    response = client.execute(httpGet);
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8").trim();
                    JSONObject json = new JSONObject(result);
                    String code = json.getString("code");

                    if (null != engineeringOrderListForMapListener) {
                        if (code.equals(EngineeringUtil.HTTP_SUCCESSED_COD)) {
                            JSONArray jsonArr = new JSONArray(json.getString("data"));
                            List<OrderParentBean> allOrderList = new ArrayList<OrderParentBean>();
                            List<OrderParentBean> maintenanceOrderBeanList = new ArrayList<OrderParentBean>();
                            List<OrderParentBean> engineeringOrderBeanList = new ArrayList<OrderParentBean>();
                            if (jsonArr != null) {
                                for (int i = 0; i < jsonArr.length(); i++) {
                                    JSONObject jsonObj = jsonArr.getJSONObject(i);
                                    if (jsonObj != null) {
                                        OrderParentBean orderBean = new OrderParentBean();

                                        orderBean.setEngineeringAddress(jsonObj.getString("engineeringAddress"));
                                        orderBean.setId(jsonObj.getString("orderId"));

                                        orderBean.setEngineeringName(jsonObj.getString("engineeringName"));
                                        orderBean.setProjectName(jsonObj.getString("projectName"));
                                        orderBean.setItemId(jsonObj.getString("itemId"));

                                        String latStr = jsonObj.getString("lat");
                                        if (!TextUtils.isEmpty(latStr)) {
                                            orderBean.setLat(Double.parseDouble(latStr));
                                        }

                                        String lonStr = jsonObj.getString("lon");
                                        if (!TextUtils.isEmpty(lonStr)) {
                                            orderBean.setLon(Double.parseDouble(lonStr));
                                        }


                                        String state = jsonObj.getString("state");
                                        if (null != state) {
                                            if (state.equals("EXCEPTION")) {
                                                orderBean.setFeedBackState(CommonEnum.FeedBackState.EXCEPTION);
                                            } else if (state.equals("WAIT_CONFIRM")) {
                                                orderBean.setFeedBackState(CommonEnum.FeedBackState.WAIT_CONFIRM);
                                            } else if (state.equals("WORKING")) {
                                                orderBean.setFeedBackState(CommonEnum.FeedBackState.WORKING);

                                            } else if (state.equals("COMPLETE")) {
                                                orderBean.setFeedBackState(CommonEnum.FeedBackState.COMPLETE);
                                            } else if (state.equals("OUT_OF_PLAN")) {
                                                orderBean.setFeedBackState(CommonEnum.FeedBackState.OUT_OF_PLAN);
                                            } else if (state.equals("NO_FEEDBACK")) {
                                                orderBean.setFeedBackState(CommonEnum.FeedBackState.NO_FEEDBACK);
                                            } else if (state.equals("PAUSE")) {
                                                orderBean.setFeedBackState(CommonEnum.FeedBackState.PAUSE);

                                            }else if(state.equals("CREATE")){
                                                orderBean.setFeedBackState(CommonEnum.FeedBackState.CREATE);
                                            }
                                        }

                                        String category = jsonObj.getString("engineeringDeptOrderCategoryEnum");
                                        if (null != category) {
                                            if (category.equals("INSTALL")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.INSTALL);
                                                engineeringOrderBeanList.add(orderBean);
                                            } else if (category.equals("BATCH_INSTALL")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.BATCH_INSTALL);
                                                engineeringOrderBeanList.add(orderBean);
                                            } else if (category.equals("CONSTRUCT")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.CONSTRUCT);
                                                engineeringOrderBeanList.add(orderBean);
                                            } else if (category.equals("MAINTAIN")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.MAINTAIN);
                                                engineeringOrderBeanList.add(orderBean);
                                            } else if (category.equals("PUBLICATION")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.PUBLICATION);
                                                maintenanceOrderBeanList.add(orderBean);
                                            } else if (category.equals("PATROL")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.PATROL);
                                                maintenanceOrderBeanList.add(orderBean);
                                            }else if (category.equals("SURVEY")) {
                                                orderBean.setCategory(CommonEnum.EngineeringORderCategory.SURVEY);
                                                engineeringOrderBeanList.add(orderBean);
                                            }
                                        }

                                        allOrderList.add(orderBean);


                                    }
                                }
                            }


                            engineeringOrderListForMapListener.getEngineeringOrderListForMapSuccessed(allOrderList, maintenanceOrderBeanList, engineeringOrderBeanList);
                        } else {
                            String error_str = code;
                            error_str += "," + ConstantUtil.getErrorCode(code);
                            if (!json.isNull("error_str")) {
                                error_str += "," + json.getString("error_str");
                            }

                            engineeringOrderListForMapListener.getEngineeringOrderListForMapFailed(error_str);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {

                }

            }
        }).start();
    }

    public interface OnEngineeringOrderListForMapListener {
        void getEngineeringOrderListForMapSuccessed(List<OrderParentBean> allOrderList, List<OrderParentBean> matenanceOrderList, List<OrderParentBean> engineeringManageOrderList);

        void getEngineeringOrderListForMapFailed(String desc);
    }
}
